import JSEncrypt from "jsencrypt";

// 密钥对生成 http://web.chacuo.net/netrsakeypair

const publicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4vHvbvbIKlPgXXzEEUuiErrrt
5AhbC4TRl3o06l8OqZJU6J6fLQFbzrtxJmzlYEs9yRXz09O9qKRCA0OSNmMKWmNC
NUJGXHgRozMtWNqQkVT78EKCBGmShWZ0eOebaVtSvUx23sNKAs/ySaSkv5WCoQPo
4mLBKrzZZE8LTC8nawIDAQAB
-----END PUBLIC KEY-----`;

const privateKey = `-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALi8e9u9sgqU+Bdf
MQRS6ISuuu3kCFsLhNGXejTqXw6pklTonp8tAVvOu3EmbOVgSz3JFfPT072opEID
Q5I2YwpaY0I1QkZceBGjMy1Y2pCRVPvwQoIEaZKFZnR455tpW1K9THbew0oCz/JJ
pKS/lYKhA+jiYsEqvNlkTwtMLydrAgMBAAECgYEAtjo/F2CE6slN/IgK6w/5i0Bs
5lMFJwIyzyjUBLSYdfcxgI5CQhP7/hrsbU4RJyaYsebUv8NZYAGlnfRoSCdcA2F8
G0e0WRQK1DivvswOz5cPTJ88fkcxtMIYxfbh/DUNN9sxv2aBDXxDMM2icUWlWJvV
xMD1UZt3IJ+D4NmrC5kCQQDoBTJ+5wMz8FD4QHDIzmklRVa8QUgXCBNN/Z6zIvSW
DUWU1y0wBqds/3eJGMKKK2wY9q4oyWVyVd0c3jllCojdAkEAy9Q+WoOyy9Hk3ygS
SvSdBpZfYgHcnqssHQSn0osQwiMBxYEM74gxe7w9ZquHMUIJMInzP6jU42SKtGSs
WzXI5wJBAItlQ/c2N5hm016y6I/cYxN/WyLzXaposaBztXsOjz/vbVSFCqMF/GTh
/yEg/RAiEmnVGsFqbkaJC7UiVf08nQkCQDmA2kp85x87St9uFaMR2Y73M1eaANy+
m8Zf/tyWIRcz+xb2LmkROiyXUZc6gvxxjdgkwvxNrCmuvrbfxARDazcCQFKxgeBT
qu0VLekJ/y812reLu9IgqglzHUfvikS10XBGKndPC61p7huerOwzlOIN0S6QSexQ
K33ue0peoJz6Vec=
-----END PRIVATE KEY-----`;

// 加密
export function encrypt(txt: string) {
  const encryptor = new JSEncrypt();
  encryptor.setPublicKey(publicKey); // 设置公钥

  const result = encryptor.encrypt(txt); // 对数据进行加密
  return result ? result : "";
}

// 解密
export function decrypt(txt: string) {
  const encryptor = new JSEncrypt();
  encryptor.setPrivateKey(privateKey); // 设置私钥
  return encryptor.decrypt(txt); // 对数据进行解密
}
